372C - Watching Fireworks is Fun - CodeForces Solution


data structures dp math *2100

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <queue>
#include <cmath>

using namespace std;

typedef long long int LL;

const LL N = 2e5, INF = 1e18;
LL dp[2][N], n;
deque <int> kol;

inline void push(int x) {
	if(kol.empty()) {
		kol.push_back(x);
		return;
	} while(!kol.empty() && dp[0][kol.back()] <= dp[0][x])
		kol.pop_back();
	kol.push_back(x);
}

inline void count_dp(int a, int b, LL dst) {
	for(int i = 0; i <= n; i++) {
		dp[0][i] = dp[1][i];
		dp[1][i] = -INF;
	} for(int i = 1; i < min(n + 1, dst); i++)
		push(i);
	for(int i = 1; i <= n; i++) {
		if(!kol.empty() && kol.front() < i - dst)
			kol.pop_front();
		if(i + dst <= n)
			push(i + dst);
		dp[1][i] = dp[0][kol.front()] + b - abs(a - i);
	} while(!kol.empty())
        kol.pop_back();
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	LL d, m, last = 0, ans = -INF; cin >> n >> m >> d;
	for(int i = 0; i < m; i++) {
		int a, b, t; cin >> a >> b >> t;
		count_dp(a, b, d * (t - last));
		last = t;
	} for(int i = 1; i <= n; i++)
		ans = max(ans, dp[1][i]);
	cout << ans << "\n";
	return 0;
}


Comments

Submit
0 Comments
More Questions

1399A - Remove Smallest
208A - Dubstep
1581A - CQXYM Count Permutations
337A - Puzzles
495A - Digital Counter
796A - Buying A House
67A - Partial Teacher
116A - Tram
1472B - Fair Division
1281C - Cut and Paste
141A - Amusing Joke
112A - Petya and Strings
677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas
32B - Borze
1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math
832A - Sasha and Sticks
292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home
617A - Elephant
48A - Rock-paper-scissors
294A - Shaass and Oskols